/*
 * Copyright (c) 2019，2039, bugu and/or its affiliates. All rights reserved.
 *
 */

package com.lbx.dhr.user.web;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.lbx.dhr.user.application.dto.TestDto;
import com.lbx.dhr.user.application.serivce.TestService;
import com.lbx.dhr.user.infra.entity.Test;
import com.lbx.dhr.user.infra.repo.TestRepo;
import com.lbx.framework.common.api.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * todo 写下本controller 你的注释
 *
 * @author ${author}
 * @version 1.0.0
 * @createdAt ${createDate}
 * @updatedAt ${createDate}
 */
@Api(tags = "")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    TestRepo testRepo;
    @Autowired
    TestService testService;

    /**
     *  查询全量数据， 默认会添加分页
     *
     * @param
     * @return
     * @author alei
     * @date 2024/1/20 17:49
     */
    @ApiOperation(value = "查询全部")
    @RequestMapping(value = "/getAll", method = {RequestMethod.GET, RequestMethod.POST})
    public Resp<List<TestDto>> getAll(Integer pageNum, Integer pageSize, @RequestBody(required = false) Test test) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }

        List<TestDto> res = testService.getAll();
        return Resp.success(res);
    }


    /**
     * 条件查询, 分页
     *
     * @param
     * @return
     * @author ${author}
     * @createdAt ${createDate}
     * @updatedAt ${createDate}
     */
    @ApiOperation(value = "条件查询")
    @RequestMapping(value = "/findByCondition", method = {RequestMethod.GET, RequestMethod.POST})
    public Resp<PageInfo<Test>> findByCondition(Integer pageNum, Integer pageSize, @RequestBody(required = false) Test test) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }

        PageInfo<Test> pageInfo = testRepo.findByConditionWithPage(pageNum, pageSize, test);
        return Resp.success(pageInfo);
    }

    /**
     * 保存
     * 有id为更新
     * 没有id为新增
     *
     * @param
     * @return
     * @author ${author}
     * @createdAt ${createDate}
     * @updatedAt ${createDate}
     */
    @ApiOperation(value = "保存和更新")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Resp<String> save(@RequestBody Test test) {
        String id = test.getId();
        if (null == id) {
            testRepo.insert(test);
            id = test.getId();
        } else {
            testRepo.updateById(test);
        }
        return Resp.success(id);
    }


    /**
     * 获取详情
     *
     * @param
     * @return
     * @author ${author}
     * @createdAt ${createDate}
     * @updatedAt ${createDate}
     */
    @ApiOperation(value = "主键查询， 不建议使用， 替换方案： findByUcode")
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Resp<Test> findById(String id) {
        Test test = testRepo.findById(id);
        return Resp.success(test);
    }


    /**
     * 删除
     *
     * @param
     * @return
     * @author ${author}
     * @createdAt ${createDate}
     * @updatedAt ${createDate}
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Resp<Boolean> deleteById(String id) {
        Preconditions.checkArgument(id != null, "ID不能为空");
        int count = testRepo.deleteById(id);
        return Resp.success(count == 1);
    }
}

